//渲染列表
const articleObj = {
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
}
let total = 0  //取到请求的列表数据的总数 后期在分页哪里渲染
async function getArticleList() {

    $(".layui-table tbody").empty()
 

    const res = await articleList('article/list', articleObj)
    console.log( res.data.total);
    total = res.data.total
   

    res.data.data.forEach(obj => {
        var theTr = `<tr>
                        <td>${obj.title}</td>
                        <td>${obj.cate_name}</td>
                        <td>${moment(obj.pub_date).format('YYYY-MM-DD, h:mm:ss')}</td>
                        <td>${obj.state}</td>
                        <th>
                        <a href="/iframe/article/publisher/publisher.html?id=${obj.Id}" class="layui-btn layui-btn-xs">编辑</a>
                        <button myid="${obj.Id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger del">删除</button>
                            </th>
                    </tr>`;
        $(".layui-table tbody").append(theTr);
    })
    setPage()

}
getArticleList()


//  渲染selec的下拉文字
async function getSelect() {
    $('select[name=cate_id]').empty()
    $(`<option >所有分类</option>`).appendTo('select[name=category]')
    const res = await indexAxios('article/cates')

    res.data.data.forEach(item => {
        $(`<option value="${item.Id}" >${item.name}</option>`).appendTo('select[name=category]')
        layui.form.render('select', 'category');
    })
}

getSelect()


//点击submit按钮  筛选文章列表   只需要把 cate_id: '',state: ''  传给服务器 在渲染 就可以达到筛选的目的
$('.search').on('submit', function (e) {
    e.preventDefault()
    // 传给服务器
    // articleObj.cate_id= $('select[name="category"]').val()
    // articleObj.state=$('select[name="state"]').val()

    const articleObj = {
        pagenum: 1,
        pagesize: 5,

        cate_id: $('select[name="category"]').val(),
        state: $('select[name="state"]').val()
    }
    getArticleList()
})

//分页
//完整功能

function setPage(){
    layui.laypage.render({
        elem: 'page'
        , count: total
        , limit: articleObj.pagesize
        , limits: [2, 4, 8, 10]
        , cuur: articleObj.pagenum
        , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
        , jump: function (obj,first) {
            // console.log(obj)
            if(!first){

                articleObj.pagenum=obj.cuur
                articleObj.pagesize=obj.limit
                // getArticleList()
            }
        }
    });
}



